## 本脚本功能为将jpg转png图片

import os
from PIL import Image


def convert_jpg_to_png_and_delete(input_dir):
    # 遍历目录及其所有子目录
    for root, dirs, files in os.walk(input_dir):
        for file in files:
            # 判断文件是否为jpg格式
            if file.lower().endswith(".jpg"):
                jpg_path = os.path.join(root, file)
                png_path = os.path.join(root, file.rsplit(".", 1)[0] + ".png")

                try:
                    # 打开jpg文件并转换为png
                    with Image.open(jpg_path) as img:
                        img.save(png_path, "PNG")
                        print(f"Converted: {jpg_path} -> {png_path}")

                    # 删除原来的jpg文件
                    os.remove(jpg_path)
                    print(f"Deleted: {jpg_path}")

                except Exception as e:
                    print(f"Error processing {jpg_path}: {e}")


if __name__ == "__main__":
    input_dir = r"/home/shuai/ros_boat_ws/src/app_interface/map/OfflineMap/tiles/satellite"  # 请修改为你目标文件夹的路径
    convert_jpg_to_png_and_delete(input_dir)
